package com.mg.admin.service.admin.impl;

import com.mg.admin.bean.admin.Result;
import com.mg.admin.bean.admin.RoleVO;
import com.mg.admin.bean.admin.UserVO;
import com.mg.admin.constants.Constants;
import com.mg.admin.constants.ResultEnum;
import com.mg.admin.dao.admin.ResourceMapper;
import com.mg.admin.dao.admin.RoleMapper;
import com.mg.admin.dao.admin.UserMapper;
import com.mg.admin.page.PageBean;
import com.mg.admin.page.PageParam;
import com.mg.admin.service.admin.UserService;
import com.xiaoleilu.hutool.crypto.SecureUtil;
import com.xiaoleilu.hutool.lang.Editor;
import com.xiaoleilu.hutool.util.CollectionUtil;
import com.xiaoleilu.hutool.util.StrUtil;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service("userServiceImpl")
public class UserServiceImpl extends BaseService implements UserService {

    @Autowired
    private UserMapper userMapper;

	@Autowired
	private ResourceMapper resourceMapper;

	@Autowired
	private RoleMapper roleMapper;

	@Override
	public UserVO selectById(Long id) {
		UserVO userVO = userMapper.selectById(id);
		return userVO;
	}

	/**
	 * 根据账号密码登录验证
	 * @param userno
	 * @param password
	 * @return
	 */
	@Override
	public Result<UserVO> login(String userno, String password) {
		UserVO userVO = userMapper.selectByNameAndPwd(userno,password);
		if (userVO == null){
			return new Result<>(ResultEnum.USERNAME_PASSWORD_ERROR);
		}else {
			Set<String> roleList = new HashSet<>();
			Set<String> permissionsById = new HashSet<>();
			String resourceIds = "";
			//如果用户账号是admin,赋予全部权限
			if (userVO.getUsername().equals(Constants.ADMIN)){
				roleList.add("*");
				permissionsById.add("*.*");
				resourceIds = null;
				userVO.setOrgId((long) Constants.ONE);
			}else{
				//根据角色id，查询角色
				RoleVO roleVO = roleMapper.findById(userVO.getRoleId());
				//根据角色查询，资源
				if (StrUtil.isNotEmpty(roleVO.getResourceIds()))  resourceIds = roleVO.getResourceIds();
				permissionsById = resourceMapper.findPermissionsById(resourceIds);
				permissionsById = (Set<String>) CollectionUtil.filter(permissionsById, new Editor<String>() {
					@Override
					public String edit(String s) {
						if (StrUtil.isNotBlank(s)){
							return s.trim();
						}else {
							return null;
						}
					}
				});

				roleList.add(roleVO.getRole());
			}

			userVO.setRoles(roleList);
			userVO.setPermissions(permissionsById);
			userVO.setResourceIds(resourceIds);
			
			return new Result<>(userVO);
		}

	}

	@Override
	public UserVO findByExample(UserVO userVO) {
		UserVO vo = userMapper.findByExample(userVO);
		return vo;
	}

	@Override
	public PageBean findByParentOrgId(Long orgId, PageParam pageParam) {
		RowBounds rb = new RowBounds(pageParam.getStart(),pageParam.getNumPerPage());
		List<UserVO> doList = userMapper.findByParentOrgId(orgId,rb);
		int totalCount = userMapper.getCountByParentOrgId(orgId,rb);
		PageBean pageBean = new PageBean(pageParam.getCurrentPage(),pageParam.getNumPerPage(),totalCount,doList);
		return pageBean;
	}


	@Override
	@Transactional
	public void insertOne(UserVO userVO) {
		userVO.setPassword(SecureUtil.md5(userVO.getPassword()));
		userMapper.insertOne(userVO);
		userMapper.insertUserRoleOrg(userVO);
	}

	@Override
	@Transactional
	public void updateOne(UserVO userVO) {
		if (StrUtil.isEmpty(userVO.getPassword())){
			userVO.setPassword(null);
		}else {
			userVO.setPassword(SecureUtil.md5(userVO.getPassword()));
		}
		userMapper.updateOne(userVO);
		userMapper.updateUserRoleOrg(userVO);
	}

	@Override
	public UserVO findByUserName(String username) {
		UserVO userVO = userMapper.findByUserName(username);
		return userVO;
	}

	@Override
	@Transactional
	public void delete(Long id) {
		userMapper.delete(id);
		userMapper.deleteUserRoleOrg(id);
	}

	@Transactional
	@Override
	public void updateByUserName(UserVO userVO) {
		userMapper.updateByUserName(userVO);
	}

}